package com.simon.batteryinformation;

import android.annotation.SuppressLint;
import android.app.Activity;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class BatteryDataShow extends ListActivity {
	private BatteryDBAdapter mDbHelper;
	private static final int CONF_ID = 1;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.datalist);

		mDbHelper = new BatteryDBAdapter(this);
		mDbHelper.open();
		fillData();
	}

	/**
	 * Fill list.
	 */
	private void fillData() {
		Cursor notesCursor = mDbHelper.fetchAllGpsInfos();
		startManagingCursor(notesCursor);

		DataAdapter batteryData = new DataAdapter(this, notesCursor,
				getConfiguration());
		setListAdapter(batteryData);
	}

	@SuppressLint("ParserError")
	private Map<String, Boolean> getConfiguration() {

		Map<String, Boolean> conf = new HashMap<String, Boolean>();

		SharedPreferences SP = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		boolean id = SP.getBoolean("id", false);
		boolean date = SP.getBoolean("date", false);
		boolean volt = SP.getBoolean("volt", false);
		boolean health = SP.getBoolean("health", false);
		boolean level = SP.getBoolean("level", false);
		boolean temp = SP.getBoolean("temp", false);

		conf.put("id", id);
		conf.put("date", date);
		conf.put("volt", volt);
		conf.put("health", health);
		conf.put("level", level);
		conf.put("temp", temp);
		return conf;
	}

	@Override
	protected void onDestroy() {
		mDbHelper.close();
		super.onDestroy();
	}

	/**
	 * Create activity option in menu.
	 **/
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, CONF_ID, 0, R.string.listConfiguration);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case CONF_ID:
			configuration();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	/**
	 * Run new activity that shows saved information about the battery. New
	 * activity BatteryDataShow.
	 */
	private void configuration() {
		Intent i = new Intent(this, ColumnPreferences.class);
		startActivity(i);
	}

	/**
	 * Restart activity after change configuration.
	 */
	@Override
	public void onRestart() {
		super.onRestart();
		//Intent intent = getIntent();
		//finish();
		//startActivity(intent);
	}

	@Override
	public void onStart() {
		super.onStart();
		Log.v("TEST", "ONSTART");
	}

	@Override
	public void onResume() {
		super.onResume();
		Log.v("TEST", "ONRESUME");
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.v("TEST", "ONSTOP");
	}
}